import { useReducer } from 'react';

/** 定义初始状态信息  */
interface IstateType {
  copeWithData: Record<string, any>[];
  collectionData: Record<string, any>[];
}

export enum IContractHPActionEnum {
  set_copeWithData = 'copeWithData',
  set_collectionData = 'collectionData',
}

interface IActionType {
  type: IContractHPActionEnum;
  payload?: any;
}

/** 状态仓库 */
const useContractHPReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    copeWithData: [],
    collectionData: [],
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      case IContractHPActionEnum.set_copeWithData:
        return {
          ...state,
          copeWithData: action.payload,
        };
      case IContractHPActionEnum.set_collectionData:
        return {
          ...state,
          collectionData: action.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, dispatch] = useReducer(updateReducer, initState);

  return {
    ...state,
    dispatch,
  };
};

export { useContractHPReducer };
